﻿/************************************************************************
 * CoreDES - A small and core library for discrete event systems
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     CoreDES\Utils\BaseModel.cs
 * Description  :     
 * Change log   :     -Oct 2013: initial version
 * To Do        :
 ************************************************************************/

using System;
using System.Collections.Generic;


namespace CoreDES.Modeling
{
    /// <summary>
    /// Interface for base model
    /// </summary>
    public interface IBaseModel<RuleType, SvType>
        where SvType : IVariable
        where RuleType : IRule
    {
        List<SvType> GetVariables();

        SvType GetStateVar(string svID);

        List<RuleType> GetRules();

        RuleType GetRule(string ruleID);

        void Init();

        string Name
        {
            get;
        }
    }

    /// <summary>
    /// Automata.Base model
    /// </summary>
    /// <typeparam name="RuleType">Type of rules/actions</typeparam>
    /// <typeparam name="SvType">Type of state variables</typeparam>
    public abstract class BaseModel<RuleType, SvType>
        //: IBaseModel
        where SvType : IVariable
        where RuleType : IRule
    {
        /// <summary>
        /// The set of rules
        /// </summary>
        protected List<RuleType> rules;

        /// <summary>
        /// The set of variables
        /// </summary>
        protected List<SvType> variables;

        /// <summary>
        /// Constructor
        /// </summary>
        public BaseModel()
        {
            rules = new List<RuleType>();
            variables = new List<SvType>();
        }

        /// <summary>
        /// The specific method to initialize the model
        /// </summary>
        public abstract void Initialize();


        /// <summary>
        /// Get a rule by id
        /// </summary>
        /// <param name="ruleID"></param>
        /// <returns></returns>
        public RuleType GetRule(string ruleID)
        {
            foreach (RuleType rule in rules)
                if (rule.Name == ruleID)
                    return rule;
            return default(RuleType);
        }

        /// <summary>
        /// Get a state variable by id
        /// </summary>
        /// <param name="svID"></param>
        /// <returns></returns>
        public SvType GetStateVar(string svID)
        {
            foreach (SvType sv in variables)
                if (sv.Name == svID)
                    return sv;
            return default(SvType);
        }
    }
}
